import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '岗位',			name: 'project_id', type: 'select' },
		],
		// 提交表单列表
		formData : [
			{ label: '公司抬头',			name: 'main_title', 		full:true, required:true, },
			{ label: '二级抬头',			name: 'secondary_title', 	full:true },
			{ label: '二级英文抬头',		name: 'subtitle', 			full:true },
			{ label: '客服电话',			name: 'customer_phone', 	full:true },
			{ label: '公司地址',			name: 'address', 			full:true },
			{ label: '岗位',				name: 'project_id', 		half:true,type:'select' },
			{ label: '状态',				name: 'enabled', 			half:true, type:'select', data:[{ name:'开启', value: '1' },{ name:'未开启', value: '0' },{ name:'关闭', value: '-1' }] },
			{ label: 'logo',			name: 'logo_img', 			half:true, type:'upload', code:118 },
			{ label: '二维码',			name: 'qr_code', 			half:true, type:'upload', code:118 },
			{ label: '公章',				name: 'official_seal', 		half:true, type:'upload', code:118 },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		this.getModalSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 岗位
			$fn.setRow(forms, 'project_id',  await f.getCache({ api: 'gang_wei' }))
			this.setState({ forms })
		})
	}
	//获取弹窗下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			$fn.setRow(formData, 'project_id',  await f.getCache({ api: 'gang_wei' }))
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'conf-report/index')
	
	// 表单
	cols = [
		{ title: '公司抬头', 		dataIndex: 'main_title', 		width:150 },
		{ title: '二级抬头', 		dataIndex: 'secondary_title', 		width:150 },
		{ title: '二级英文抬头', 		dataIndex: 'subtitle', 			width:200 },
		{ title: '公司地址', 		dataIndex: 'address', 			width:250 },
		{ title: '岗位', 			dataIndex: 'project_name', 		width:100 },
		{ title: '状态', 			dataIndex: 'enabled', 			width:80,  render: text => {
			return +text === 1 ? '开启' : '禁用'
		} },
		TableControls.controls.call(this,{ width: 100 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'conf-report/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),															// 删除
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect}),			// 添加
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'conf-report/del', { uuid, isKeys: !uuid })
	render(){
		return (
			<>
				<Page title='报告单抬头' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'conf-report/add', editApi:'conf-report/edit' }) }
			</>
		)
	}
}

export default Index